import Vue from 'vue'
import VueRouter from 'vue-router'
import to from 'await-to-js'

import Home from '@/views/Home.vue'
import About from '@/views/About.vue'
import {NavigatorItemArray} from '@/utils/navigator-item.js'

Vue.use(VueRouter)

var routeOfViews = []
NavigatorItemArray.forEach((nav) => {
  if (nav.children.length == 0) {
    routeOfViews.push({
      path: `/${nav.key}`,
      name: `view-${nav.key}`,
      component: async () => {
        let err, mod
        [err, mod] = await to(import(`@/views/view-${nav.key}.vue`))
        if (err) {
          // console.log(err)
          return Home
        }
        else {
          // console.log(mod)
          return mod
        }
      }
    })
  }
})

var routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: About
  }
]
routes = routes.concat(routeOfViews)

const router = new VueRouter({
  routes
})

export default router
